/*

This file is part of Rooms3D.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
Rooms3D is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
Rooms3D is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with Rooms3D; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

package com.datasentinel.rooms.ui.sh3d;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;

import com.datasentinel.file.FileBuilderFactory;
import com.datasentinel.rooms.swing.util.Dialogs;
import com.eteks.sweethome3d.model.RecorderException;

public class Main
{
	final private static String APP_NAME = "Rooms3D dataSentinel File Viewer";
	private static final Logger logger = Logger.getLogger(Main.class.getName());
	private Rooms3DController rooms3DController;

	public Main(Container contentPane)
	{
		rooms3DController = new Rooms3DController(contentPane);
	}

	public Main(JFrame frame)
	{
		this(frame.getContentPane());
		try
		{
			loadDefaultHome();
		}
		catch (RecorderException e)
		{
			logger.log(Level.SEVERE, "Error opening default room", e);
		}
	}

	private void loadDefaultHome() throws RecorderException
	{
		try
		{
			File userHome = FileBuilderFactory.createFileBuilder().fileHome();
			rooms3DController.loadHome(userHome.getName(), userHome);
		}
		catch (IOException e)
		{
			throw new RecorderException("Error loading default home", e);
		}
	}


	private void saveHome() throws RecorderException
	{
		rooms3DController.saveHome();
	}

	/**
	 * @param args
	 */
	public static void main(final String[] args)
	{
		final JFrame frame = new JFrame(APP_NAME);
		frame.setSize(800, 600);
		frame.getContentPane().setLayout(new BorderLayout());
		final Main main = new Main(frame.getContentPane());
		EventQueue.invokeLater(new Runnable() {
			public void run()
			{
				try
				{
					main.loadDefaultHome();
				}
				catch (Exception e)
				{
					Dialogs.displayException("Error opening default room", e);
					frame.dispose();
					System.exit(1);
				}
			}
		});
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent we)
			{
				try
				{
					main.saveHome();
				}
				catch (Exception e)
				{
					Dialogs.displayException("Error opening default room", e);
				}
				frame.setVisible(false);
				System.exit(0);
			}
		});
		frame.setVisible(true);
	}
}
